package pagedto

import (
	"gitee.com/ichub/webcli/common/base/basedto"
	"gitee.com/ichub/webcli/common/base/baseutils"
)

type FieldSign string

const (
	Eq = iota
	Ne
	Ge
	Gt
	Le
	Lt
	In
	Like
	Between
	NotIn
	NotLike
	NotBetween
)

var OpSign = map[int]FieldSign{
	Eq: "=",
	Ne: "!=",
	Ge: ">=",
	Gt: ">",
	Le: "<=",
	Lt: "<",

	In:         "in",
	Like:       "like",
	Between:    "between",
	NotIn:      "not in",
	NotLike:    "not like",
	NotBetween: "not between",
}

type IchubQueryField struct {
	basedto.BaseEntity
	Field string `json:"field"`

	OpType FieldSign     `json:"op_type"`
	Values []interface{} `json:"values"`
}

func NewQueryFields() *IchubQueryField {
	var bfields = &IchubQueryField{}
	bfields.InitProxy(bfields)
	return bfields
}
func NewFields(field string, opType int, opValues []interface{}) *IchubQueryField {

	var bfields = &IchubQueryField{
		Field:  field,
		OpType: OpSign[opType],
		Values: opValues,
	}
	bfields.InitProxy(bfields)
	return bfields
}
func (this *IchubQueryField) CheckType() string {
	return baseutils.CheckType(this.Values[0])
}

func (this *IchubQueryField) SetField(field string) {
	this.Field = field
}

func (this *IchubQueryField) Values2InStr() string {
	var s = ""
	for _, v := range this.Values {
		if len(s) > 0 {
			s += ","
		}
		if this.CheckType() == "string" {
			s += `"` + baseutils.Any2Str(v) + `"`
		} else {
			s += baseutils.Any2Str(v)
		}

	}
	return s

}
